﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ADO.NET.Samples.Forms
{
    [Common.FormDescription("Custom Bingding Format", "Demonstrate how to customize binding format.")]
    public partial class BindingFormatAndParameterizedQuery : Form
    {
        public BindingFormatAndParameterizedQuery()
        {
            InitializeComponent();
        }

        private void BindingFormatAndParameterizedQuery_Load(object sender, EventArgs e)
        {
            var binding = this.textBoxUserName.DataBindings["Text"];
            binding.Format += new ConvertEventHandler(binding_Format);
            binding.Parse += new ConvertEventHandler(binding_Parse);
            // TODO: This line of code loads data into the 'dBDataSet.User' table. You can move, or remove it, as needed.
            this.userTableAdapter.FillUsers(this.dBDataSet.User);
        }

        /// <summary>
        /// Parse data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void binding_Parse(object sender, ConvertEventArgs e)
        {
            e.Value = e.Value.ToString().Substring(4);
        }

        /// <summary>
        /// Use customized binding format
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void binding_Format(object sender, ConvertEventArgs e)
        {
            e.Value = string.Format("TEST_{0}", e.Value);
        }

        /// <summary>
        /// Save user data
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButtonSave_Click(object sender, EventArgs e)
        {
            this.bindingSourceUsers.EndEdit();
            this.userTableAdapter.Update(this.dBDataSet);
        }
    }
}
